package com.halflife.freemarker;

import freemarker.template.TemplateModelException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration;
import org.springframework.boot.autoconfigure.freemarker.FreeMarkerProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * Created by zhaoyf on 2018/2/25.
 */
@Configuration
public class FreemarkerConfiguration {
    //    FreeMarkerAutoConfiguration.
    private Logger log = LoggerFactory.getLogger(FreemarkerConfiguration.class);
    @Autowired
    protected freemarker.template.Configuration configuration;
    @Autowired
    protected org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver resolver;
    @Autowired
    protected org.springframework.web.servlet.view.InternalResourceViewResolver springResolver;
    @Value("${system.freemarker.base}")
    public String base;
    @Value("${system.freemarker.jsUrl}")
    public String cssUrl;
    @Value("${system.freemarker.cssUrl}")
    public String jsUrl;
    @Value("${system.freemarker.baseUrl}")
    public String baseUrl;

    @PostConstruct
    public void setShareVariable() {
        try {
            configuration.setSharedVariable("jsUrl", base+jsUrl);
            configuration.setSharedVariable("cssUrl", base+cssUrl);
            configuration.setSharedVariable("baseUrl",base+ baseUrl);
            configuration.setSharedVariable("base",base);
        } catch (TemplateModelException e) {
            log.error(e.toString(), e);
        }
    }

}
